<?php 
	Class Hotel extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model("model_hotel");
            $this->load->helper(array('form', 'url'));
		}
		
		public function index(){
			$provide='';
			if(isset($_GET['provide'])){
				if($_GET['provide']=="0") $provide='';
				else $provide=$_GET['provide'];
			}
			$name='';
			if(isset($_GET['name'])) $name=$_GET['name'];
			
			$data["hotel"]=$this->model_hotel->getallbyProvide_Name($provide,$name);
			
			$data["template"]="hotel/hotel_manager";
			$this->load->view("layout",$data);
		}
		public function edit(){
			
			if(isset($_GET["id"])) $data["detail"]=$this->model_hotel->getdata(intval($_GET["id"]));
			else $data['detail']=NULL;
			
			$data["template"]="hotel/hotel_edit";
			$this->load->view("layout",$data);
		}
		public function update(){
			if(isset($_GET["id"])){			
				$data['address']=$_POST['address'];	
				$data['name']=$_POST['name'];	
				$data['overview']=$_POST['overview'];	
				$data['description']=$_POST['description'];	
				$data['category']=$_POST['category'];	
				$data['provide']=$_POST['provide'];	
				$data['star']=$_POST['star'];	
				$data['policy']=$_POST['policy'];	
				$data['hotelfacility']=$_POST['hotelfacility'];	
				$data['businessfacility']=$_POST['businessfacility'];	
				$data['recreationalfacility']=$_POST['recreationalfacility'];	
				$data['guestreview']=$_POST['guestreview'];	
				$data['restaurant']=$_POST['restaurant'];	
				$data['nearby']=$_POST['nearby'];	
				$data['lat']=$_POST['lat'];	
				$data['long']=$_POST['long'];	
				$data['status']=$_POST['status'];
                if($_FILES['fuavatar']['name'] != NULL){
                    $config['upload_path'] = './uploads/hotel';
                    $config['allowed_types'] = '*';
                    $config['max_size']	= '3000';
                    $config['max_width']  = '2704';
                    $config['max_height']  = '2700';
                    $this->load->library('upload',$config);
                    if(!$this->upload->do_upload("fuavatar")){
                        redirect('admin/hotel/index','refresh');
                        return FALSE;
                    }else{
                        $dataimg = $this->upload->data();
                        $data['avatar'] = $dataimg['file_name'];
                    }
                }
				$this->model_hotel->update($data,intval($_GET["id"]));
				header('Location: '.base_url().'admin/hotel/index');
			}
		}
		
		public function insert(){			
			$data['address']=$_POST['address'];	
			$data['name']=$_POST['name'];	
			$data['overview']=$_POST['overview'];	
			$data['description']=$_POST['description'];	
			$data['category']=$_POST['category'];	
			$data['provide']=$_POST['provide'];	
			$data['star']=$_POST['star'];	
			$data['policy']=$_POST['policy'];	
			$data['hotelfacility']=$_POST['hotelfacility'];	
			$data['businessfacility']=$_POST['businessfacility'];	
			$data['recreationalfacility']=$_POST['recreationalfacility'];	
			$data['guestreview']=$_POST['guestreview'];	
			$data['restaurant']=$_POST['restaurant'];	
			$data['nearby']=$_POST['nearby'];	
			$data['lat']=$_POST['lat'];	
			$data['long']=$_POST['long'];	
			$data['status']=$_POST['status'];
            if($_FILES['fuavatar']['name'] != NULL){
                $config['upload_path'] = './uploads/hotel';
                $config['allowed_types'] = '*';
                $config['max_size']	= '3000';
                $config['max_width']  = '2704';
                $config['max_height']  = '2700';
                $this->load->library('upload',$config);
                if(!$this->upload->do_upload("fuavatar")){
					header('Location: '.base_url().'admin/hotel/index');
                    return FALSE;
                }else{
                    $dataimg = $this->upload->data();
                    $data['avatar'] = $dataimg['file_name'];
                }
            }
			$this->model_hotel->add($data);	
			
			header('Location: '.base_url().'/admin/hotel/index');
		}
		
		public function delete(){		
			if(isset($_GET["id"])){
                $data = $this->model_hotel->getdata($_GET['id']);
                @unlink("uploads/hotel/".$data['avatar']);
                $this->model_hotel->del(intval($_GET["id"]));
				header('Location: '.base_url().'admin/hotel/index');
			}
		}
        public function createThumbnail($fileName,$path,$width,$height){
            $this->load->library('image_lib');
            $config['image_library'] = 'gd2';
            $config['source_image'] = 'uploads/'.$path.'/'.$fileName;
            $config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['thumb_marker'] = FALSE;
            $config['width'] = $width;
            $config['height'] = $height;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();
        }
		
		public function changestatus(){
			if(isset($_GET['status'])){
				$data['status']=intval($_GET['status']);
				$this->model_hotel->update($data,intval($_GET['id']));					
				$provide='';
				if(isset($_GET['provide'])) $provide='?provide='.$_GET['provide'];
				$name='';
				if(isset($_GET['name'])) $name='&name='.$_GET['name'];
				header('Location: '.base_url().'admin/hotel/index'.$provide.$name);
			}	
		}
	}
?>